﻿using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace POC_002.Controls
{
    [
    ToolboxData("<{0}:PteTestControl runat=\"server\"> </{0}:PteTestControl>")
    ]
    public class PteTestControl : CompositeControl
    {
        private Label _testLabel1;
        private Label _testLabel2;
        private TextBox _textBox1;
        private TextBox _textBox2;

        protected override void CreateChildControls()
        {
            Controls.Clear();

            _testLabel1 = new Label {Text = "Test 1"};
            _testLabel2 = new Label {Text = "Test 2"};
            
            _textBox1 = new TextBox();
            _textBox1.ID = "Txt1";
            _textBox1.Attributes.Add("placeHolder", "TextBox 1");
            _textBox2 = new TextBox();
            _textBox2.ID = "Txt2";
            _textBox2.Attributes.Add("placeHolder", "TextBox 2");

            Controls.Add(_testLabel1);
            Controls.Add(_textBox1);
            Controls.Add(_testLabel2);
            Controls.Add(_textBox2);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            writer.Write(ReturnJavascript());
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "form-horizontal");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); //main div
            
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-group");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); //div1
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-label");
            _testLabel1.RenderControl(writer);
            
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "controls");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); //div1-1
            _textBox1.RenderControl(writer);
            writer.RenderEndTag(); //div1-1
            writer.RenderEndTag(); //div1

            writer.AddAttribute("class", "control-group");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); //div2
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-label");
            _testLabel2.RenderControl(writer);

            writer.AddAttribute("class", "controls");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); //div2-1
            _textBox2.RenderControl(writer);
            writer.RenderEndTag(); //div2-1
            writer.RenderEndTag(); //div2

            writer.RenderEndTag(); //close main div
        }

        private string ReturnJavascript()
        {
            StringBuilder sb = new StringBuilder();
            const string mainScript = "<script>{0}</script>";
            const string documentReady = "$(document).ready(function() {0} );";
            const string datePicker = "$('#_|0|_').datepicker({ showOn: 'button', buttonImage: 'images/calendar.gif', buttonImageOnly: true });";

            string processed = datePicker.Replace("_|0|_", _textBox1.ClientID);
            sb.Append("{");
            sb.Append(processed);
            sb.Append("}");
            processed = string.Format(documentReady, sb.ToString());
            processed = string.Format(mainScript, processed);
            return processed;
        }
    }
}